import java.util.concurrent.Semaphore;

/**
 * 1115. 交替打印FooBar
 * https://leetcode-cn.com/problems/print-foobar-alternately/
 */
public class Solutions_1115 {
    public static void main(String[] args) {
//        int n = 2;  // foobarfoobar
        int n = 3;  // foobarfoobarfoobar

        FooBar obj = new FooBar(n);

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    obj.foo(new Runnable() {
                        @Override
                        public void run() {
                            System.out.print("foo");
                        }
                    });
                } catch(InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    obj.bar(new Runnable() {
                        @Override
                        public void run() {
                            System.out.print("bar");
                        }
                    });
                } catch(InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();
        t1.start();
    }
}

/**
 * 解法二：信号量实现
 */
class FooBar {
    private int n;
    private Semaphore s1;
    private Semaphore s2;

    public FooBar(int n) {
        this.n = n;
        s1 = new Semaphore(0);
        s2 = new Semaphore(0);
    }

    public void foo(Runnable printFoo) throws InterruptedException {

        for (int i = 0; i < n; i++) {
            // s1 获取信号量，无信号量将阻塞
            s1.acquire();
            // printFoo.run() outputs "foo". Do not change or remove this line.
            printFoo.run();
            // 线程执行后，给 s2 释放出一个信号量，使线程 2 可执行
            s2.release();
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {

        for (int i = 0; i < n; i++) {
            // 给 s1 释放出一个信号量，使线程 1 可执行
            s1.release();
            // s2 获取信号量，无信号量将阻塞
            s2.acquire();
            // printBar.run() outputs "bar". Do not change or remove this line.
            printBar.run();
        }
    }
}

/**
 * 解法二：synchronized 实现
 */
class FooBar2 {
    private int n;
    private boolean flag = false;
    private Object obj = new Object();

    public FooBar2(int n) {
        this.n = n;
    }

    public void foo(Runnable printFoo) throws InterruptedException {

        for (int i = 0; i < n; i++) {
            synchronized(obj) {
                while (flag) {
                    obj.wait();
                }
                // printFoo.run() outputs "foo". Do not change or remove this line.
                printFoo.run();
                flag = !flag;
                obj.notify();
            }
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {

        for (int i = 0; i < n; i++) {
            synchronized(obj) {
                while (!flag) {
                    obj.wait();
                }
                // printBar.run() outputs "bar". Do not change or remove this line.
                printBar.run();
                flag = !flag;
                obj.notify();
            }
        }
    }
}
